﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Standard;
using Sector7.Framework.Utility;
using Sector7.Medical.EntityType;
using Sector7.Framework.Enums;
using Sector7.Medical.ADDM.Windows;

namespace Sector7.Medical.ADDM.Controls
{
    /// <summary>
    /// InterviewSummaryControl.xaml 的交互逻辑
    /// </summary>
    public partial class InterviewSummaryControl : UserControl
    {
        public InterviewSummaryControl()
        {
            InitializeComponent();
        }

        private void GetInterviewTemplateList()
        {
            m_templateList = DAOFactory.GetDAO<InterviewTemplateDAO>().ReadAll();
            ClientUtil.LoadItemsSource(dtgTemplate, m_templateList);
        }

        private void GetInterviewTemplateItemList(int templateId)
        {
            var itemList = DAOFactory.GetDAO<InterviewTemplateItemDAO>().ReadAll(templateId);
            ClientUtil.LoadItemsSource(dtgTemplateItem, itemList);
        }

        private void LoadInterviewTemplate(T7_InterviewTemplate template)
        {
            if (template != null)
            {
                txbTemplateName.Text = template.TemplateName;
                txbPercentOfInterview.Text = template.PercentOfInterview.ToString();
                txbPercentOfWritten.Text = template.PercentOfWritten.ToString();
                var itemList = template.Items;

                if (itemList == null)
                {
                    itemList = DAOFactory.GetDAO<InterviewTemplateItemDAO>().ReadAll(template.TemplateId);

                    m_templateList.Find(delegate(T7_InterviewTemplate __template)
                    {
                        if (__template.TemplateId == template.TemplateId)
                        {
                            __template.Items = itemList;

                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });
                }

                ClientUtil.LoadItemsSource(dtgTemplateItem, template.Items);
            }
            else
            {
                txbTemplateName.Text = null;
                txbPercentOfInterview.Text = null;
                txbPercentOfWritten.Text = null;

                ClientUtil.UnLoadItemsSource(dtgTemplateItem);
            }
        }

        private void dtgTemplate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedTemplate = dtgTemplate.SelectedItem as T7_InterviewTemplate;

            LoadInterviewTemplate(selectedTemplate);
        }

        private void miTemplate_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            var crud = (CrudType)Enum.Parse(typeof(CrudType), menuItem.Tag.ToString());
            var selectedTemplate = dtgTemplate.SelectedItem as T7_InterviewTemplate;
            bool? result = false;

            switch (crud)
            {
                case CrudType.Create:
                    var addTemplateWindow = new AddInterviewTemplateWindow();
                    addTemplateWindow.Crud = crud;
                    result = addTemplateWindow.ShowDialog();

                    if (result == true)
                    {
                        GetInterviewTemplateList();
                    }
                    break;
                case CrudType.Update:
                    if (selectedTemplate != null)
                    {
                        var modifyTemplateWindow = new AddInterviewTemplateWindow();
                        modifyTemplateWindow.Crud = crud;
                        modifyTemplateWindow.InterviewTemplate = selectedTemplate;
                        result = modifyTemplateWindow.ShowDialog();

                        if (result == true)
                        {
                            GetInterviewTemplateList();
                        }
                    }
                    break;
                case CrudType.Delete:
                    break;
                default:
                    break;
            }
        }

        private void miTemplateItem_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            var crud = (CrudType)Enum.Parse(typeof(CrudType), menuItem.Tag.ToString());
            var selectedTemplate = dtgTemplate.SelectedItem as T7_InterviewTemplate;
            if (selectedTemplate != null)
            {
                int templateId = selectedTemplate.TemplateId;
                var selectedItem = dtgTemplateItem.SelectedItem as T7_InterviewTemplateItem;
                bool? result = false;

                switch (crud)
                {
                    case CrudType.Create:
                        var addItemWindow = new AddInterviewTemplateItemWindow();
                        addItemWindow.Crud = crud;
                        addItemWindow.InterviewTemplate = selectedTemplate;
                        result = addItemWindow.ShowDialog();

                        if (result == true)
                        {
                            GetInterviewTemplateItemList(templateId);
                        }
                        break;
                    case CrudType.Update:
                        if (selectedItem != null)
                        {
                            var modifyItemWindow = new AddInterviewTemplateItemWindow();
                            modifyItemWindow.Crud = crud;
                            modifyItemWindow.InterviewTemplate = selectedTemplate;
                            modifyItemWindow.InterviewTemplateItem = selectedItem;
                            result = modifyItemWindow.ShowDialog();

                            if (result == true)
                            {
                                GetInterviewTemplateItemList(templateId);
                            }
                        }
                        break;
                    case CrudType.Delete:
                        break;
                    default:
                        break;
                }
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetInterviewTemplateList();
        }

        private List<T7_InterviewTemplate> m_templateList = null;
    }
}
